



function addMaterialToSellPrice(){

	//得到要添加记录的表格---tbody
	var mbody = document.getElementById("mbody");
	//向表格中增加一行记录
	var tr = mbody.insertRow(mbody.rows.length) ;
	//表示行的下标
	var rowIndex = mbody.rows.length-1 ;
	
	//增加一个单元格
	
	var cell = tr.insertCell(0); 
	
	cell.innerHTML = "<td><input type='hidden' name='sellpricedetail' id='sellpricedetail"+rowIndex+"'><input type='checkbox' name='checkbox'></td>" ;
	//物料代码
	cell = tr.insertCell(1);
	
	cell.innerHTML = "<td><input type='hidden' name ='materialId' id='materialId"+rowIndex+"'> <input name='productCode' id='materialCode"+rowIndex +"'type='text' size='10' ondblclick='openWinMaterial("+rowIndex+",800,600,1)'></td>" ;
	//物料名称
	cell = tr.insertCell(2) ;
	
	cell.innerHTML = "<td><input type='text' size='10' readonly='true' name ='productName' id='materialName"+rowIndex+"'></td>" ;
	//规格型号
	cell = tr.insertCell(3);
	
	cell.innerHTML = "<td><input type='text' size='10' readonly='true' name ='standardType' id='spec"+rowIndex+"'></td>" ;
	//基本单位名称
	cell = tr.insertCell(4);
	
	cell.innerHTML = "<td><input type='text' size='10' readonly='true' name ='baseUnitName' id='basicMeasureUnitName"+rowIndex+"'></td>" ;
	//基本单位数量(从)
	cell = tr.insertCell(5) ;
	
	cell.innerHTML = "<td><input type='text' size='10' readonly='true' name='baseUnitNoStart' id='baseUnitNoStart"+rowIndex+"'></td>" ;
	//基本单位数量(到)
	cell = tr.insertCell(6) ;
	
	cell.innerHTML = "<td><input type='text' size='10' readonly='true' name='baseUnitNoEnd' id='baseUnitNoEnd"+rowIndex+"'></td>" ;
	//单位
	cell = tr.insertCell(7);
	
	cell.innerHTML = "<td><input type='hidden' name = 'measureUnitId' id='measureUnitId"+rowIndex+"'><input type='text' size='10' name='unit' id='measureUnitName"+rowIndex+"' ondblclick='openWinMeasureUnit(" + rowIndex + ",800,600,0)'></td>";

	//数量（从）
	cell = tr.insertCell(8);
	
	cell.innerHTML = "<td><input type='text' size='10' name='amountStart' id='amountStart"+rowIndex+"'onblur='count("+rowIndex+")'></td>" ;
	
	//数量（到）
	cell = tr.insertCell(9);
	
	cell.innerHTML = "<td><input type='text' size='10' name='amountEnd' id='amountEnd"+rowIndex+"'onblur='count("+rowIndex+")'></td>" ;
	
	//单价
	cell = tr.insertCell(10);
	
	cell.innerHTML = "<td><input type='text' size='10' name='unitPrice' id='unitPrice"+rowIndex+"'onblur='count("+rowIndex+")'></td>" ;
	
	//含税单价
	cell = tr.insertCell(11);
	
	cell.innerHTML = "<td><input type='text' size='10' name='addTaxUnitPrice' id='addTaxUnitPrice"+rowIndex+"'></td>" ;
	
	//税率
	cell = tr.insertCell(12);
	
	cell.innerHTML = "<td><input type='text' size='10' name='taxRate' id='taxRate"+rowIndex+"'></td>" ;
	
	//折扣率
	cell = tr.insertCell(13);
	
	cell.innerHTML = "<td><input type='text' size='10' name='discountRate' id='discountRate"+rowIndex+"'onblur='count("+rowIndex+")'></td>" ;
	
	//实际含税单价
	cell = tr.insertCell(14);
	
	cell.innerHTML = "<td><input type='text' size='10'  readonly='true' name='realAddTaxUnitPrice' id='realAddTaxUnitPrice"+rowIndex+"'></td>" ;

	//备注
	cell = tr.insertCell(15);
	
	cell.innerHTML = "<td><input type='text' size='10' name='remark' id = 'remark'></td>" ;
	
}



function openWinMaterial(index, width, height, scroll) {
	//alert(this.parentElement.parentElement.rowIndex);
	var truelink = "selectForSellPrice.action?rowIndex="+index;
	openWin(truelink, null, width, height, scroll);
}


function openWinMeasureUnit(index, width, height, scroll){
	var truelink = "selectMeasure.action?rowIndex="+index;
	openWin(truelink, null, width, height, scroll);
}


function childToParentSellPrice(index,id, childId, name, childName, code, childCode, spec, childSpec,baseMeasureUnit,childBaseMeasureUnit) {
	var t = window.opener.document.getElementById("mbody");	
	//物料id
	var parentId = window.opener.document.getElementById(id +index);
	//物料name
	var parentName = window.opener.document.getElementById(name+ index);
	//物料code
	var parentCode = window.opener.document.getElementById(code + index);
	//规格型号
	var parentSpec = window.opener.document.getElementById(spec+ index);
	//基本计量单位
	var parentBaseMeasureUnit =  window.opener.document.getElementById(baseMeasureUnit+ index);
	//基本单位数量(从)
	var baseUnitNoStart =  window.opener.document.getElementById('baseUnitNoStart'+ index);
	//基本单位数量(到)
	var baseUnitNoEnd =  window.opener.document.getElementById('baseUnitNoEnd'+ index);
	//单位
	var unit = window.opener.document.getElementById('measureUnitName'+index);
//	var parentMeasureUnitId = window.opener.document.getElementById('measureUnitId'+ index);
	//数量（从）
	var amountStart = window.opener.document.getElementById('amountStart'+index);
	//数量（到）
	var amountEnd = window.opener.document.getElementById('amountEnd'+index);
	//单价
	var unitPrice = window.opener.document.getElementById('unitPrice'+index);
	//含税单价
	var addTaxUnitPrice = window.opener.document.getElementById('addTaxUnitPrice'+index);
	
	//税率
	var taxRate = window.opener.document.getElementById('taxRate'+index);
	//折扣率
	var discountRate = window.opener.document.getElementById('discountRate'+index);
	
	//实际含税单价
	var realAddTaxUnitPrice = window.opener.document.getElementById('realAddTaxUnitPrice'+index);
	
	//备注
	var remark = window.opener.document.getElementById('remark'+index);
	
	
	parentId.value = childId;
	parentName.value = childName;
	parentCode.value = childCode;
	parentSpec.value = childSpec;
	parentBaseMeasureUnit.value = childBaseMeasureUnit;
	
//	unit.value = childBaseMeasureUnit;
//	parentMeasureUnitId.value = childMeasureUnitId;
	
	baseUnitNoStart.value = '0.00';
	baseUnitNoEnd.value = '0.00';
	amountStart.value = '0.00';
	amountEnd.value = '0.00';
	unitPrice.value = '0.00';
	addTaxUnitPrice.value = '0.00';
	taxRate.value = '17.00';
	discountRate.value = '0.00';
	realAddTaxUnitPrice.value = '0.00';
	
	window.close();
}


//计算相关数值
function count (rowIndex){
	//数量（从）
	var amountStart = document.getElementById('amountStart'+rowIndex);
	var camountStart = parseFloat(amountStart.value);

	//数量（到）
	var amountEnd = document.getElementById('amountEnd'+rowIndex);
	var camountEnd = parseFloat(amountEnd.value);

	//单价
	var unitPrice = document.getElementById('unitPrice'+rowIndex);
	var cunitPrice = parseFloat(unitPrice.value);

	//税率
	var taxRate = document.getElementById('taxRate'+rowIndex);
	var ctaxRate = parseFloat(taxRate.value);
	
	//含税单价
	var addTaxUnitPrice = document.getElementById('addTaxUnitPrice'+rowIndex);
	var caddTaxUnitPrice = parseFloat(addTaxUnitPrice.value);

	//折扣
	var discountRate = document.getElementById('discountRate'+rowIndex);
	var cdiscountRate = parseFloat(discountRate.value);

	//实际含税单价
	var realAddTaxUnitPrice = document.getElementById('realAddTaxUnitPrice'+rowIndex);
	var crealAddTaxUnitPrice = parseFloat(realAddTaxUnitPrice.value);

	//单位
	var measureUnitName = document.getElementById('measureUnitName'+rowIndex);
	var cmeasureUnitName = parseFloat(measureUnitName.value);
	
	
	if(camountStart >= 0.0 && camountEnd > 0.0){
		if(camountEnd < camountStart){
			alert("数量（到）应大于数量（从）");
		}
	}
	
	if(cunitPrice > 0.0){
		
		addTaxUnitPrice.value = cunitPrice*(1+ctaxRate/100);
		if(cdiscountRate > 0.0){
			realAddTaxUnitPrice.value = addTaxUnitPrice.value*(1-cdiscountRate/100);
			realAddTaxUnitPrice.value = Math.round(realAddTaxUnitPrice.value*100)/100;
		}
	}
	
	if(camountStart > 0.0){
		getBaseUnitNoStartInfo(rowIndex);
	}
	
	if(camountEnd > 0.0){
		getBaseUnitNoEndInfo(rowIndex);
	}
}


